--- 模块功能：u8g2demo
-- @module u8g2
-- @author Dozingfiretruck
-- @release 2021.01.25

local sys = require("sys")

--[[ 注意：如需使用u8g2的全中文字库需将 luat_base.h中26行#define USE_U8G2_WQY12_T_GB2312 打开]]

-- 项目信息,预留
VERSION = "1.0.0"
PRODUCT_KEY = "1234567890"

-- 日志TAG, 非必须
local TAG = "main"
local last_temp_data = "0"

--配置WIFI的ssid与password
local ssid = "xuxiaoyong"
local password = "Xx13945094998"
-- local ssid = "HUAWEI-WQMXQG"
-- local password = "szqhgszn"

--天气API
local weather_host = "http://www.luatos.com/api/public/weather2?city="  --天气host
local weather_location = "哈尔滨"                                       --获取天气位置

local weather_now,temperature_now
local weather_2,temperature_2
local weather_3,temperature_3

--天气HTTP回调函数
local function weather_cbfnc(code,headers,body)
    if body then
        local jsondata,ret,errinfo = json.decode(body)
        if ret and type(jsondata)=="table" then
            local city = jsondata["city"]
            weather_now = jsondata["live"]["weather"]
            temperature_now = jsondata["live"]["temperature"]
            local weather_data = "城市:"..city.." 天气:"..weather_now.." 温度:"..temperature_now
            print("weather",weather_data)
            weather_now = jsondata["forecasts"][2]["dayweather"]
            temperature_now = jsondata["forecasts"][2]["daytemp"]

            weather_2 = jsondata["forecasts"][2]["dayweather"]
            temperature_2 = jsondata["forecasts"][2]["daytemp"]
            print("weather2",weather_2..temperature_2)
            weather_3 = jsondata["forecasts"][2]["dayweather"]
            temperature_3 = jsondata["forecasts"][2]["daytemp"]
            print("weather3",weather_3..temperature_3)

            sys.publish("WEATHER_OK")
        else
            print("estJson.decode error",errinfo)
        end
    end
end

local function drawWeatherSymbol(x, y,symbol)
    if symbol=="多云" then
        u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
        u8g2.DrawGlyph(x, y, 64)
    elseif symbol=="霾" then
        u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
        u8g2.DrawGlyph(x, y, 65)
    -- elseif symbol=="月亮" then
    --     u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
    --     u8g2.DrawGlyph(x, y, 66)
    elseif symbol=="雨"or symbol=="雪" then
        u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
        u8g2.DrawGlyph(x, y, 67)
    -- elseif symbol=="星星" then
    --     u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
    --     u8g2.DrawGlyph(x, y, 68)
    elseif symbol=="晴" then
        u8g2.SetFont("u8g2_font_open_iconic_weather_6x_t")
        u8g2.DrawGlyph(x, y, 69)
    end
end

-- 联网主流程
local function init()
    wlan.setMode("wlan0", wlan.STATION)
    -- 初始化显示屏
    log.info(TAG, "init ssd1306")
    u8g2.begin({mode="i2c_hw",i2c_id=0})
    --u8g2.begin({mode="i2c_sw", pin0=18, pin1=19})
    u8g2.SetFontMode(1)
    u8g2.ClearBuffer()
    u8g2.SetFont("u8g2_font_ncenB08_tr")
    u8g2.DrawUTF8("U8g2+LuatOS", 32, 28)
    u8g2.SendBuffer()
    for i=0,30,5 do
        u8g2.ClearBuffer()
        u8g2.SetFont("u8g2_font_ncenB08_tr")
        u8g2.DrawUTF8("U8g2+LuatOS", 32, i)
        u8g2.SendBuffer()
    end
    for i=128,34,-6 do
        u8g2.ClearBuffer()
        u8g2.SetFont("u8g2_font_ncenB08_tr")
        u8g2.DrawUTF8("U8g2+LuatOS", 32, 28)
        u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
        u8g2.DrawUTF8("by 打盹的消防车", i, 55)
        u8g2.SendBuffer()
    end
    sys.wait(2000)
    wlan.connect(ssid, password)
    u8g2.ClearBuffer()
    u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
    u8g2.DrawUTF8("WiFi连接中", (u8g2.GetDisplayWidth()-5*12)/2, 28)
    u8g2.DrawUTF8("请稍后...", (u8g2.GetDisplayWidth()-4*12)/2, 45)
    u8g2.SendBuffer()
    sys.waitUntil("WLAN_READY", 30000)
    if wlan.ready() then
        u8g2.ClearBuffer()
        u8g2.DrawUTF8("WIFI连接成功", (u8g2.GetDisplayWidth()-6*12)/2, 38)
        u8g2.SendBuffer()
        sys.wait(2000)
    else
        u8g2.ClearBuffer()
        u8g2.DrawUTF8("WIFI连接失败", (u8g2.GetDisplayWidth()-6*12)/2, 38)
        u8g2.SendBuffer()
        sys.wait(1000)
    end

    while socket.isReady()~=true do
        u8g2.ClearBuffer()
        u8g2.DrawUTF8("网络连接中", (u8g2.GetDisplayWidth()-5*12)/2, 28)
        u8g2.DrawUTF8("请稍后...", (u8g2.GetDisplayWidth()-4*12)/2, 45)
        u8g2.SendBuffer()
    end
    u8g2.ClearBuffer()
    u8g2.DrawUTF8("网络连接成功", (u8g2.GetDisplayWidth()-6*12)/2, 38)
    u8g2.SendBuffer()
    sys.wait(1000)
    socket.ntpSync()
    u8g2.ClearBuffer()
    u8g2.DrawUTF8("时间同步中", (u8g2.GetDisplayWidth()-5*12)/2, 28)
    u8g2.DrawUTF8("请稍后...", (u8g2.GetDisplayWidth()-4*12)/2, 45)
    u8g2.SendBuffer()
    sys.waitUntil("NTP_UPDATE", 50000)

    http.get(weather_host..weather_location.."&imei="..wlan.getMac(), nil, weather_cbfnc)
    sys.waitUntil("WEATHER_OK", 6000)

    while true do
        local timetable = os.date("*t", os.time())
        u8g2.ClearBuffer()
        u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
        u8g2.DrawUTF8("时钟", (u8g2.GetDisplayWidth()-2*12)/2, 15)
        u8g2.DrawUTF8(string.format("%02d年%02d月%02d日",timetable.year,timetable.month,timetable.day),28, 30)
        u8g2.DrawUTF8(string.format("%02d:%02d:%02d",timetable.hour,timetable.min,timetable.sec),45, 45)
        u8g2.SendBuffer()
        sys.wait(1500)

        u8g2.ClearBuffer()
        drawWeatherSymbol(10, 60,weather_now)
        u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
        u8g2.DrawUTF8("当前天气", (u8g2.GetDisplayWidth()-4*12)/2, 10)
        u8g2.DrawUTF8("天气: "..weather_now, 65, 35)
        u8g2.DrawUTF8("温度: "..temperature_now.."℃", 65, 50)
        u8g2.SendBuffer()
        sys.wait(1500)

        u8g2.ClearBuffer()
        drawWeatherSymbol(10, 60,weather_now)
        u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
        u8g2.DrawUTF8("明天天气", (u8g2.GetDisplayWidth()-4*12)/2, 10)
        u8g2.DrawUTF8("天气: "..weather_2, 65, 35)
        u8g2.DrawUTF8("温度: "..temperature_2.."℃", 65, 50)
        u8g2.SendBuffer()
        sys.wait(1500)

        u8g2.ClearBuffer()
        drawWeatherSymbol(10, 60,weather_now)
        u8g2.SetFont("u8g2_font_wqy12_t_gb2312")
        u8g2.DrawUTF8("后天天气", (u8g2.GetDisplayWidth()-4*12)/2, 10)
        u8g2.DrawUTF8("天气: "..weather_3, 65, 35)
        u8g2.DrawUTF8("温度: "..temperature_3.."℃", 65, 50)
        u8g2.SendBuffer()
        sys.wait(1500)

    end
end

-- TODO: 用户按钮(PB7), 用于清除配网信息,重新airkiss

-- TODO: 联网更新脚本和底层(也许)
sys.taskInit(init)
-- 主循环, 必须加
sys.run()
